﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LDSP.BRL;
using LDSP.Util.Constants;
using System.IO;
using System.Web;

namespace LDSP.Util
{
    public class FavoriteStandardExport : CodificationExportBase<R_EnterpriseCatalog, R_EnterpriseCodification>
    {
        private int _enterpriseId;
        private string _templateName;
        HttpServerUtility _server;
        private IDictionary<string, int> _columnOrderDic = new Dictionary<string, int>();

        public FavoriteStandardExport(int enterpriseID, HttpServerUtility server, string templateName)
        {
            _enterpriseId = enterpriseID;
            _server = server;
            _templateName = templateName;

            InitialColumnOrder(templateName);
        }

        private void InitialColumnOrder(string templateName)
        {
            //包含冒号的，是绝对路径；否则是相对路径。
            string fileName = templateName.Contains(":") ? templateName : _server.MapPath("~\\Templates\\Codification\\" + templateName);

            ExcelClass cls = new ExcelClass(fileName);
            DataTable dt = cls.ToDataTable(0);
            foreach (DataColumn col in dt.Columns)
            {
                _columnOrderDic.Add(col.ColumnName, col.Ordinal);
            }
        }

        protected override void InitialRows(DataTable dt, IList<R_EnterpriseCodification> codifications)
        {
            foreach (R_EnterpriseCodification cod in codifications)
            {
                DataRow row = dt.NewRow();
                //优先级
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Priority))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.Priority]] = cod.Priority.ToString();
                }
                //目录
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Category))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.Category]] = cod.Codification.Category.Name;
                }
                //名称
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Title))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.Title]] = cod.Codification.Name;
                }
                //发布机构
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.PublishAgency))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.PublishAgency]] = cod.Codification.Organization;
                }
                //发布文号
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.PublishNumber))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.PublishNumber]] = cod.Codification.ReferenceNumber;
                }
                //发布时间
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.PublishDate))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.PublishDate]] = cod.Codification.ReleaseDT;
                }
                //生效时间
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.EffectiveDate))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.EffectiveDate]] = cod.Codification.ImplementDT;
                }
                //失效列表
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.InvalidList))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.InvalidList]] = !string.IsNullOrEmpty(cod.Codification.InvalidDescription) ? cod.Codification.InvalidDescription : GenerateInvalidList(cod.Codification); ;
                }
                //责任人
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Principle))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.Principle]] = cod.Principal;
                }
                //概述
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Summary))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.Summary]] = cod.Name;
                }
                //适用条款及要求
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.KeyItems))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.KeyItems]] = cod.KeyItems;
                }
                //实施措施
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Implementation))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.Implementation]] = cod.Action;
                }
                //不符合内容
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Implementation))
                {
                    row[_columnOrderDic[FavoriteCodificationColumnNames.ExistingGap]] = cod.ExistingGap;
                }
                //自我评估
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.SelfEvaluation))
                {
                    row[_columnOrderDic[FavoriteStandardColumnNames.SelfEvaluation]] = cod.SelfEvaluation;
                }
                //计划完成时间
                if (_columnOrderDic.ContainsKey(FavoriteCodificationColumnNames.PlanFinishDate))
                {
                    if (cod.PlanFinishDT != DateTime.MaxValue)
                    {
                        row[_columnOrderDic[FavoriteCodificationColumnNames.PlanFinishDate]] = cod.PlanFinishDT;
                    }
                }
                //实际完成时间
                if (_columnOrderDic.ContainsKey(FavoriteCodificationColumnNames.ActualFinishDate))
                {
                    if (cod.ActualFinishDT != DateTime.MaxValue)
                    {
                        row[_columnOrderDic[FavoriteCodificationColumnNames.ActualFinishDate]] = cod.ActualFinishDT;
                    }
                }
                //状态
                if (_columnOrderDic.ContainsKey(FavoriteCodificationColumnNames.Status))
                {
                    row[_columnOrderDic[FavoriteCodificationColumnNames.Status]] = cod.Status.ToString();
                }
                //备注
                if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Comments))
                {
                    row[_columnOrderDic[FavoriteCodificationColumnNames.Comments]] = cod.Comment;
                }
                dt.Rows.Add(row);
            }
        }

        protected override void InitialColumns(DataTable dt)
        {
            dt.Columns.Add("Priority");
            dt.Columns.Add("Category");
            dt.Columns.Add("Title");
            dt.Columns.Add("Agency");
            dt.Columns.Add("OrderNumber");
            dt.Columns.Add("IssueDate");
            dt.Columns.Add("EffectiveDate");
            dt.Columns.Add("Invalidate");
            dt.Columns.Add("Principal");
            dt.Columns.Add("Summary");
            dt.Columns.Add("KeyPoints");
            dt.Columns.Add("ImplementationSteps");
            dt.Columns.Add("ExistingGap");
            dt.Columns.Add("SelfEvaluation");
            dt.Columns.Add("Comments");
            dt.Columns.Add("PlanFinishDate");
            dt.Columns.Add("ActualFinishDate");
            dt.Columns.Add("Status");

            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Priority))
            {
                dt.Columns["Priority"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.Priority]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Category))
            {
                dt.Columns["Category"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.Category]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Title))
            {
                dt.Columns["Title"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.Title]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.PublishAgency))
            {
                dt.Columns["Agency"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.PublishAgency]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.PublishNumber))
            {
                dt.Columns["OrderNumber"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.PublishNumber]);
            } if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.PublishDate))
            {
                dt.Columns["IssueDate"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.PublishDate]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.EffectiveDate))
            {
                dt.Columns["EffectiveDate"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.EffectiveDate]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.InvalidList))
            {
                dt.Columns["Invalidate"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.InvalidList]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Principle))
            {
                dt.Columns["Principal"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.Principle]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Summary))
            {
                dt.Columns["Summary"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.Summary]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.KeyItems))
            {
                dt.Columns["KeyPoints"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.KeyItems]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.Implementation))
            {
                dt.Columns["ImplementationSteps"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.Implementation]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.ExistingGap))
            {
                dt.Columns["ExistingGap"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.ExistingGap]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteStandardColumnNames.SelfEvaluation))
            {
                dt.Columns["SelfEvaluation"].SetOrdinal(_columnOrderDic[FavoriteStandardColumnNames.SelfEvaluation]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteCodificationColumnNames.Comments))
            {
                dt.Columns["Comments"].SetOrdinal(_columnOrderDic[FavoriteCodificationColumnNames.Comments]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteCodificationColumnNames.PlanFinishDate))
            {
                dt.Columns["PlanFinishDate"].SetOrdinal(_columnOrderDic[FavoriteCodificationColumnNames.PlanFinishDate]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteCodificationColumnNames.ActualFinishDate))
            {
                dt.Columns["ActualFinishDate"].SetOrdinal(_columnOrderDic[FavoriteCodificationColumnNames.ActualFinishDate]);
            }
            if (_columnOrderDic.ContainsKey(FavoriteCodificationColumnNames.Status))
            {
                dt.Columns["Status"].SetOrdinal(_columnOrderDic[FavoriteCodificationColumnNames.Status]);
            }
        }

        protected override IList<R_EnterpriseCatalog> GetAllCatalogs()
        {
            return new R_EnterpriseCatalog().GetListByEnterpriseID(_enterpriseId); ;
        }

        protected override void GenerateList(IDictionary<string, DataTable> dataTables, R_EnterpriseCatalog cata)
        {
            IList<R_EnterpriseCodification> codifications = new R_EnterpriseCodification().GetList(_enterpriseId, cata.CatalogID);

            string codName = GetFullName(cata.Catalog);
            dataTables.Add(codName, ConstructDT(codifications));
            DataTable dt = null;
        }
    }
}
